package com.dnjn.happiness.survey.controller.common;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.survey.domain.Check;
import com.dnjn.happiness.survey.domain.vo.CheckVO;
import com.dnjn.happiness.survey.service.ICheckItemService;
import com.dnjn.happiness.survey.service.ICheckQuestionService;
import com.dnjn.happiness.survey.service.ICheckService;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;

/**
 * 考核记录Controller
 *
 * @author happiness
 * @date 2024-04-01
 */

public class CommonCheckController extends BaseController {
    @Autowired
    protected ICheckService checkService;

    @Autowired
    protected ICheckItemService checkItemService;

    @Autowired
    protected ICheckQuestionService checkQuestionService;

    /**
     * 查询考核记录列表
     */
    @Log(title = "考核记录" , businessType = BusinessType.LIST)
//    @RequiresPermissions("@ss.hasAnyPermi('check :check:list,ignore:check:list')" )
    @PostMapping("/list" )
    public AjaxResult list(@RequestBody CheckVO vo, @RequestParam(defaultValue = "1" ) Integer current, @RequestParam(defaultValue = "10" ) Integer size) {
        Check check=new Check();
        BeanUtils.copyPropsIgnoreNull(vo,check);
        Page page = Page.of(current, size);
        Page<Check> list = checkService.page(page, Wrappers.query(check));
        return success(list);
    }

    /**
     * 获取考核记录详细信息
     */
    @Log(title = "考核记录" , businessType = BusinessType.SELECT)
//    @RequiresPermissions("@ss.hasAnyPermi('check :check:query,ignore:check:query')" )
    @GetMapping(value = "/{checkId}" )
    public AjaxResult get(@PathVariable("checkId" ) Long checkId) {
        return success(checkService.getById(checkId));
    }

    /**
     * 新增考核记录
     */
    @Log(title = "考核记录" , businessType = BusinessType.INSERT)
//    @RequiresPermissions("@ss.hasAnyPermi('check :check:add,ignore:check:add')" )
    @Operation(summary = "add新增员工考核")
    @PostMapping("/add" )
    public AjaxResult add(@RequestBody CheckVO vo) {
        Assert.notNull(vo.getCheckType(),"考核类型不能为空");

        Check check=new Check();
        BeanUtils.copyPropsIgnoreNull(vo,check);

        check.setCreateBy(SecurityUtils.getUsername());
        return toAjax(checkService.addCheck(check));
    }



    /**
     * 修改考核记录
     */
    @Log(title = "考核记录" , businessType = BusinessType.UPDATE)
//    @RequiresPermissions("@ss.hasAnyPermi('check :check:edit,ignore:check:edit')" )
    @PutMapping("/edit" )
    public AjaxResult edit(@RequestBody CheckVO vo) {
        Check check=new Check();
        BeanUtils.copyPropsIgnoreNull(vo,check);
        return toAjax(checkService.updateById(check));
    }

    /**
     * 删除考核记录
     */
    @Log(title = "考核记录" , businessType = BusinessType.DELETE)
//    @RequiresPermissions("@ss.hasAnyPermi('check :check:remove,ignore:check:remove')" )
    @DeleteMapping("/{checkIds}" )
    public AjaxResult remove(@PathVariable Long[] checkIds) {
        return toAjax(checkService.removeBatchByIds(Arrays.asList(checkIds)));
    }



}
